/******************* (C) COPYRIGHT 2022 CHU WEI TENG ***********************************************
**
**------------------ File Info ---------------------------------------------------------------------
** File name:      dwin_page.c
** modified Date:  2022-05-10
** Last Version:   V1.0
** Descriptions: 
** 
**------------------ Modfication  log --------------------------------------------------------------
** Date       Who            Rev        Comments
----------------------------------------------------------------------------------------------------
   22/05/10 chuwt@qq.com    V1.00      created

****************************************************************************************************/
#include "dwin_page.h"
#include "dwin_trans.h"

/* Private define----------------------------------------------------------------------------------*/

/* Private typedef---------------------------------------------------------------------------------*/

/* Private variables-------------------------------------------------------------------------------*/

/* Private function--------------------------------------------------------------------------------*/

static ret_t default_dwin_page_open(dwin_widget_t* page)
{
    return RET_OK;
}

static ret_t default_dwin_page_read(dwin_widget_t* page)
{
    return RET_OK;
}

static ret_t default_dwin_page_write(dwin_widget_t* page)
{
    return RET_OK;
}

static ret_t default_dwin_page_run(dwin_widget_t* page)
{
    dwin_page_t* obj = (dwin_page_t *)page;
    ret_t        result = RET_OK;
    
    if (obj->read != NULL)
    {
        result = obj->read(page);
    }
    
    if (obj->write != NULL && result == RET_OK)
    {
        result = obj->write(page);
    }
    return result;
}

static ret_t page_event_dispatch(dwin_widget_t* page, void* ctx)
{
    dwin_page_t* obj = (dwin_page_t *)page;
    data_pack_t*   pack = (data_pack_t *)ctx;
	
	obj->reg_id = (pack->addr_high << 8) | pack->addr_low;
	 
    if (page->func != NULL)
    {
        page->func(page);
    }
    return RET_OK;
}

/* public function---------------------------------------------------------------------------------*/

ret_t dwin_page_init(dwin_widget_t* page, uint16_t page_id, uint16_t addr, uint16_t attr, uint16_t size)
{
    return_value_if_fail(page, RET_BAD_PARAMS);

    dwin_page_t* obj = (dwin_page_t *)page;

    DWIN_INIT_LIST_HEAD(&obj->list);
    obj->page_id = page_id;
    obj->open = default_dwin_page_open;
    obj->read = default_dwin_page_read;
    obj->write = default_dwin_page_write;
    obj->run = default_dwin_page_run;
    obj->widget.widget_type = DWIN_PAGE;
    obj->widget.active = 1;
    obj->widget.addr = addr;
    obj->widget.attr = attr;
    obj->widget.size = size; 
    if (obj->widget.vt->dispatch == NULL)
    {
        obj->widget.vt->dispatch = page_event_dispatch;
    }
    return RET_OK;
}
ret_t dwin_page_widget_add(dwin_widget_t* page, dwin_widget_t* widget)
{
    return_value_if_fail(page, RET_BAD_PARAMS);
    return_value_if_fail(widget, RET_BAD_PARAMS);

    dwin_page_t* obj = (dwin_page_t *)page;

    dwin_list_add(&widget->list, &obj->list);

    return RET_OK;
}

ret_t dwin_page_widget_remove(dwin_widget_t* widget)
{
    return_value_if_fail(widget, RET_BAD_PARAMS);

    dwin_list_del(&widget->list);
    
    return RET_OK;
}

ret_t dwin_page_open(dwin_widget_t* page)
{
    return_value_if_fail(page, RET_BAD_PARAMS);

    ret_t        result = RET_FAIL;
    dwin_page_t* obj = (dwin_page_t *)page;

    if (obj->open != NULL)
    {
        result = obj->open(page);
    }
    return result;
}

ret_t dwin_page_read(dwin_widget_t* page)
{
    return_value_if_fail(page, RET_BAD_PARAMS);

    ret_t        result = RET_FAIL;
    dwin_page_t* obj = (dwin_page_t *)page;

    if (obj->read != NULL)
    {
        result = obj->read(page);
    }
    return result;
}

ret_t dwin_page_write(dwin_widget_t* page)
{
    return_value_if_fail(page, RET_BAD_PARAMS);

    ret_t        result = RET_FAIL;
    dwin_page_t* obj = (dwin_page_t *)page;

    if (obj->write != NULL)
    {
        result = obj->write(page);
    }
    return result;
}

ret_t dwin_page_run(dwin_widget_t* page)
{
    return_value_if_fail(page, RET_BAD_PARAMS);

    ret_t        result = RET_FAIL;
    dwin_page_t* obj = (dwin_page_t *)page;

    if (obj->run != NULL)
    {
        result = obj->run(page);
    }
    return result;
}

ret_t dwin_page_jump(dwin_widget_t* page)
{
    return_value_if_fail(page, RET_BAD_PARAMS);
    
    dwin_page_t* obj = (dwin_page_t *)page;
    
    return dwin_page_jump_id(obj->page_id);
}
ret_t dwin_page_jump_id(uint16_t page_id)
{
    uint32_t id = (0x5A01 << 16) | page_id;
    
    id = DWIN_SWAPL(id);
    
    return dwin_var_write(DWIN_SYSTEM_VAR_ADDR_JUMPPAGE, &id, 2);
}

